package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.CommentService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service // dubbo的
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer saveMovementComment(Comment comment) {
        // 1.保存comment对象
        comment.setPublishUserId(mongoTemplate.findById(comment.getPublishId(), Movement.class).getUserId()); // 动态发布人id
        mongoTemplate.save(comment);

        // 2.更新movement对象数量+1
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (comment.getCommentType() == 1) { // 动态点赞
            movement.setLikeCount(movement.getLikeCount() + 1);
        } else if (comment.getCommentType() == 2) {// 动态评论
            movement.setCommentCount(movement.getCommentCount() + 1);
        } else if (comment.getCommentType() == 3) { // 动态喜欢
            movement.setLoveCount(movement.getLoveCount() + 1);
        }
        mongoTemplate.save(movement);

        // 3.返回操作数量
        if (comment.getCommentType() == 1) { // 动态点赞
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {// 动态评论
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) { // 动态喜欢
            return movement.getLoveCount();
        }
        return 0;
    }

    @Override
    public Integer deleteMovementComment(String movementId, Long userId, Integer commentType) {
        // 1.删除comment对象
        // 1-1 组装删除条件
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(movementId)) // 动态id
                        .and("userId").is(userId) // 操作人id
                .and("commentType").is(commentType) // 操作类型
        );

        // 1-2 删除
        mongoTemplate.remove(query, Comment.class);

        // 2.更新movement对象数量-1
        Movement movement = mongoTemplate.findById(new ObjectId(movementId), Movement.class);
        if (commentType == 1) { // 动态点赞
            movement.setLikeCount(movement.getLikeCount() - 1);
        } else if (commentType == 2) {// 动态评论
            movement.setCommentCount(movement.getCommentCount() - 1);
        } else if (commentType == 3) { // 动态喜欢
            movement.setLoveCount(movement.getLoveCount() - 1);
        }
        mongoTemplate.save(movement);

        // 3.返回操作数量
        if (commentType == 1) { // 动态点赞
            return movement.getLikeCount();
        } else if (commentType == 2) {// 动态评论
            return movement.getCommentCount();
        } else if (commentType == 3) { // 动态喜欢
            return movement.getLoveCount();
        }
        return 0;
    }

    // 动态的评论列表
    @Override
    public PageBeanVo findMovementCommentByPage(Integer pageNum, Integer pageSize, String movementId) {
        // 1.组装查询条件
        Query query = new Query(
                Criteria
                        .where("publishId").is(new ObjectId(movementId)) // 动态id
                        .and("commentType").is(2) // 评论类型
        ).with(Sort.by(Sort.Order.desc("created"))) // 时间降序
                .skip((pageNum - 1) * pageSize).limit(pageSize);// 分页

        // 2.查询评论列表
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        // 3.查询总记录数
        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    @Override
    public PageBeanVo findCommentByPage(Integer pageNum, Integer pageSize, Long userId, Integer commentType) {
        // 1.组装查询条件
        Query query = new Query(
                Criteria.where("publishUserId").is(userId)
                        .and("commentType").is(commentType)
        ).skip((pageNum - 1) * pageSize).limit(pageSize);

        // 2.查询list
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        // 3.查询count
        long count = mongoTemplate.count(query, Comment.class);

        // 4.返回分页对象

        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }
}
